package com.zfast.spring5.junit;

import com.rabbitmq.client.AMQP;
import org.junit.jupiter.api.Test;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.core.env.Environment;

import javax.annotation.Resource;
import java.util.Arrays;

/**
 * @author zgn
 * @Description: simJunit
 * @date 2022/1/13 0013
 */
@SpringBootTest
public class RabbitMQJunit {
    //region 通用变量
    @Resource
    Environment env;

    @Resource
    RabbitTemplate rabbitTemplate;
    //endregion

    @Test
    public void egMin() {//e.g.
        System.out.println();
    }

    @Test
    public void egMax() {//e.g.
        //region 模拟数据
        System.out.println("--------------------Start--------------------");
        //endregion

        //region 数据处理
        System.out.println("--------------------Deal--------------------");
        //endregion

        //region 数据展示
        System.out.println("--------------------Done--------------------");
        //endregion
    }

    @Test
    public void 异步消息_egMin() {//e.g.
        String host=env.getProperty("spring.rabbitmq.host");
        String username=env.getProperty("spring.rabbitmq.username");
        String password=env.getProperty("spring.rabbitmq.password");
        String port=env.getProperty("spring.rabbitmq.port");
        CachingConnectionFactory connectionFactory = new CachingConnectionFactory(host);
        connectionFactory.setUsername(username);
        connectionFactory.setPassword(password);
        connectionFactory.setPort(Integer.valueOf(port));//AMQP.PROTOCOL.PORT

        RabbitTemplate rabbit = new RabbitTemplate(connectionFactory);
        MessageConverter converter=rabbit.getMessageConverter();
        MessageProperties props=new MessageProperties();
        Message message=converter.toMessage("发送一条消息",props);
        rabbit.send("key",message);

        System.out.println();
    }

    @Test
    public void 环境变量_egMin() {//e.g.
        System.out.println(env.getProperty("spring.rabbitmq.host"));
    }

    @Test
    public void 单播消息_egMin(){
        System.out.println("发送单播消息~");
        rabbitTemplate.convertSendAndReceive("zgn.direct","user.news",
                Arrays.asList("好像还不错哦",123,true));
    }

    @Test
    public void 接收消息_egMin(){
        Object o = rabbitTemplate.receiveAndConvert("zgn.queue");
        System.out.println(o.getClass());
        System.out.println(o);
    }


}
